package com.hymjweb.demo.framework.exception;

import com.hymjweb.demo.framework.result.BaseResultMap;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@ControllerAdvice
public class ApplicationExceptionHandler {


    @ExceptionHandler({Exception.class, UnknownAccountException.class, LockedAccountException.class})
    @ResponseBody
    public BaseResultMap exceptionHandler(Exception e){
        log.error(e.getMessage());
        e.printStackTrace();
        // 这个地方要记入文本日志
        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(false);
        baseResultMap.setMessage(e.getMessage());
        return baseResultMap;
    }


    @ExceptionHandler(value = CloneNotSupportedException.class)
    @ResponseBody
    public BaseResultMap cloneExceptionHandler(Exception e){
        log.error(e.getMessage());
        e.printStackTrace();
        // 这个地方要记入文本日志
        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(false);
        baseResultMap.setMessage("复制对象失败!");
        return baseResultMap;
    }

    @ExceptionHandler(value = DuplicateKeyException.class)
    @ResponseBody
    public BaseResultMap duplicateKeyExceptionHandler(Exception e){
        log.error(e.getMessage());
        e.printStackTrace();
        // 这个地方要记入文本日志
        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(false);
        baseResultMap.setMessage("数据表中UK重复!");
        return baseResultMap;
    }

}
